package com.anda.account.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.anda.account.constant.LoginState;
import com.anda.account.domain.model.system.User;
import com.anda.account.domain.service.UserService;
import com.anda.account.view.form.ExtJsonForm;

@Controller
@RequestMapping("user")
public class UserController {

	static Logger logger = Logger.getLogger(UserController.class.getName());
	
	@Autowired
	private UserService userService;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public static void main(String[] args) {
		
	}

	@RequestMapping("login")
	public @ResponseBody ExtJsonForm login(User user, ModelMap model){
		LoginState loginState = userService.getUserLoginState(user);
		logger.info("登录验证状态为:" + loginState);
		ExtJsonForm extJsonForm = new ExtJsonForm();
		switch (loginState) {
			case ERROR_PWD_USER: {
				extJsonForm.setSuccess(false);
				extJsonForm.setData("用户名或密码错误!");
				break;
			}
			case PASS: {
				System.out.print("userOid===>" + user.getUserId());
				user = userService.getLoginUser(user);
				model.addAttribute("currUser", user);
				extJsonForm.setSuccess(true);
				extJsonForm.setData("登录成功");
				break;
			}
		}
		return extJsonForm;
	}
	
	@RequestMapping("save")
	public @ResponseBody String save(User user){
		try{
			this.userService.save(user);
		}catch(Exception e){
			e.printStackTrace();
			return "failure";
		}
		return "";
	}
}
